<?php

namespace App\Admin\Controllers\Pets;

use App\Admin\Actions\Grid\PetsAdoptionPassAction;
use App\Admin\Repositories\Pets\WxPetsAdoption;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxPetsAdoptionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxPetsAdoption(), function (Grid $grid) {

            $grid->quickSearch(['id', 'user_id', 'name'])->placeholder('搜索id，用户id，宠物名字...')->width(35);
            $grid->model()->orderBy('id','desc');

            $grid->column('id')->sortable();
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('success_user_id');

            $grid->column('name');
            $grid->column('pictures')->display(function ($v){
                $pics = json_decode($v, true);
                if($pics){
                    return $pics[0]['url'] . '?imageMogr2/thumbnail/500x/crop/500x500/gravity/center/format/webp/interlace/0/quality/100';
                }
                return '';
            })->image('', 100, 100);
            $grid->column('species');
            $grid->column('pet_breed');
            $grid->column('is_free');
            $grid->column('cost');
            $grid->column('pet_gender');
            $grid->column('pet_age');
//            $grid->column('is_vaccine');
//            $grid->column('is_sterilization');
//            $grid->column('is_repellent');
            $grid->column('pet_introduce')->limit(20);
//            $grid->column('condition');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('district');
//            $grid->column('address');
//            $grid->column('adopter_name');
//            $grid->column('adopter_mobile');
//            $grid->column('adopter_wechatId');
//            $grid->column('adopter_ip');
            $grid->column('adopt_status')->using([0=>'推送中', 1=>'已领养', 2=>'废弃', 3 =>'审核中'])->label([
                0 => 'primary',
                1 => 'grey',
                2 => 'default',
                3 => 'red'
            ]);
            $grid->column('created_at')->sortable();
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->disableCreateButton();
            $grid->disableActions();
//            $grid->disableBatchActions();

            $grid->batchActions(function ($batch) {
                $batch->add(new PetsAdoptionPassAction());
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxPetsAdoption(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('success_user_id');

            $show->field('name');
            $show->field('pictures');
            $show->field('species');
            $show->field('pet_breed');
            $show->field('is_free');
            $show->field('cost');
            $show->field('pet_gender');
            $show->field('pet_age');
            $show->field('is_vaccine');
            $show->field('is_sterilization');
            $show->field('is_repellent');
            $show->field('pet_introduce');
            $show->field('condition');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('address');
            $show->field('adopter_name');
            $show->field('adopter_mobile');
            $show->field('adopter_wechatId');
            $show->field('adopter_ip');
            $show->field('adopt_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxPetsAdoption(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
//            $form->text('success_user_id');

            $form->text('name');
            $form->text('pictures');
            $form->text('species');
            $form->text('pet_breed');
            $form->text('is_free');
            $form->text('cost');
            $form->text('pet_gender');
            $form->text('pet_age');
            $form->text('is_vaccine');
            $form->text('is_sterilization');
            $form->text('is_repellent');
            $form->text('pet_introduce');
            $form->text('condition');
            $form->text('province');
            $form->text('city');
            $form->text('district');
            $form->text('address');
            $form->text('adopter_name');
            $form->text('adopter_mobile');
            $form->text('adopter_wechatId');
            $form->text('adopter_ip');
            $form->text('adopt_status');

            $form->display('created_at');
            $form->display('updated_at');

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
